import { View } from "../../../../mvc/core/View";
import { PageTool } from "../../../../common/global/PageTool";
import { DataEvent } from "../../../events/DataEvent";
import { SGuildPlayerAttendanceVo } from "./vo/SGuildPlayerAttendanceVo";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { GuildEventName } from "../GuildEventName";
import { MuiEvent } from "../../../../../com/mui/events/MuiEvent";
import { DisplayUtil } from "../../../../common/DisplayUtil";
import { Cache } from "../../../cache/Cache";
import { EGuildAssignType } from "../../../../../Message/Game/EGuildAssignType";
import { ListEvent } from "../../../../../fl/events/ListEvent";
import { MsgManager } from "../../../manager/MsgManager";
import { Language } from "../../../../../extend/language/Language";
import { SingleAllocationView } from "./SingleAllocationView";
import { MultiAllocationView } from "./MultiAllocationView";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { SGuildWarehouseApply } from "../../../../../Message/Game/SGuildWarehouseApply";
import { ObjCreate } from "../../common/ObjCreate";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { ImagesConst } from "../../../resource/ImagesConst";
import { ScrollPolicy } from "../../../../../fl/controls/ScrollPolicy";
import { GuildMemberItemRenderer } from "./itemRenderer/GuildMemberItemRenderer";
import { PageSelecter } from "../../common/pageSelect/PageSelecter";
import { GTabBar } from "../../../../../com/mui/controls/GTabBar";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { EventName } from "../../../mvc/EventName";
import { GTileList } from "../../../../../com/mui/controls/GTileList";
type int = number;
//class GuildResourceAllocationPanel
    
    export  class GuildResourceAllocationPanel extends View
    {
        constructor()
        {
            super();
            this._pageTool = new PageTool([], 9);
            this.init();
            return;
        }

        /* internal  */onCancelItem(arg1: DataEvent): void
        {
            var loc2=null;
            var loc1=arg1.data as SGuildPlayerAttendanceVo;
            if (loc1) 
            {
                var loc3=0;
                var loc4=this._memberData;
                for(loc2 of loc4) 
                {
                    if (loc2.itemData.miniPlayer.entityId.id != loc1.itemData.miniPlayer.entityId.id) 
                    {
                        continue;
                    }
                    loc2.isSelect = false;
                }
            }
            this.onEquipmentPageChange();
            return;
        }

        /* internal  */onShowAllocationRecordPanel(arg1: Object /* flash.events.TextEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(GuildEventName.WarehouseOpenRecordWindow));
            return;
        }

        /* internal  */onTabBarSelectedChange(arg1: MuiEvent): void
        {
            if (arg1.selectedIndex != 0) 
            {
                if (arg1.selectedIndex == 1) 
                {
                    if (this._singleAllocationView) 
                    {
                        DisplayUtil.removeMe(this._singleAllocationView);
                    }
                    this.addChild(this.multiAllocationView);
                    this._multiAllocationView.setGuildWarehouseMoney(this._storageMoneyMap);
                    Cache.instance.guild.resourceAllocationType = EGuildAssignType._EGuildAssignBatch;
                }
            }
            else 
            {
                if (this._multiAllocationView) 
                {
                    DisplayUtil.removeMe(this._multiAllocationView);
                }
                this.addChild(this.singleAllocationView);
                this._singleAllocationView.setGuildWarehouseMoney(this._storageMoneyMap);
                Cache.instance.guild.resourceAllocationType = EGuildAssignType._EGuildAssignSingle;
            }
            this.cleanSelectState();
            return;
        }

        /* internal  */onSelectItemHandler(arg1: ListEvent): void
        {
            var loc3=null;
            if (!Cache.instance.guild.isCanManageWarehouse()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(27973));
                return;
            }
            var loc1=Cache.instance.guild.resourceAllocationType;
            var loc2=arg1.item as SGuildPlayerAttendanceVo;
            if (loc2 && loc1 == EGuildAssignType._EGuildAssignSingle) 
            {
                var loc4=0;
                var loc5=this._memberData;
                for(loc3 of loc5) 
                {
                    if (loc3.itemData.miniPlayer.entityId.id == loc2.itemData.miniPlayer.entityId.id) 
                    {
                        loc3.isSelect = true;
                        SingleAllocationView(this.singleAllocationView).curSelectMemberData = loc3;
                        continue;
                    }
                    loc3.isSelect = false;
                }
            }
            else if (loc2 && loc1 == EGuildAssignType._EGuildAssignBatch) 
            {
                loc4 = 0;
                loc5 = this._memberData;
                for(loc3 of loc5) 
                {
                    if (!(loc3.itemData.miniPlayer.entityId.id == loc2.itemData.miniPlayer.entityId.id && loc3.isSelect == false)) 
                    {
                        continue;
                    }
                    loc3.isSelect = true;
                    MultiAllocationView(this.multiAllocationView).addItem(loc3);
                }
            }
            this.onEquipmentPageChange();
            return;
        }

        /* internal  */cleanSelectState(): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=this._memberData;
            for(loc1 of loc3) 
            {
                loc1.isSelect = false;
            }
            this.onEquipmentPageChange();
            return;
        }

        /* internal  */onEquipmentPageChange(arg1: Object /* flash.events.Event */=null): void
        {
            var loc1=this._memberPageSelecter.currentPage;
            this._memberList.dataProvider = new DataProvider(this._pageTool.getByPageNo(loc1));
            this._memberList.drawNow();
            return;
        }

        /* internal  */onDescBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var sortLeagueWar: Function;
            var sortBoss: Function;
            var sortRobCity: Function;

            var loc1;
            sortLeagueWar = null;
            sortBoss = null;
            sortRobCity = null;
            e = arg1;
            sortLeagueWar = (arg1: SGuildPlayerAttendanceVo, arg2: SGuildPlayerAttendanceVo): int
            {
                if (arg1.itemData.num1 > arg2.itemData.num1) 
                {
                    return -1;
                }
                if (arg1.itemData.num1 < arg2.itemData.num1) 
                {
                    return 1;
                }
                return 0;
            }
            sortBoss = (arg1: SGuildPlayerAttendanceVo, arg2: SGuildPlayerAttendanceVo): int
            {
                if (arg1.itemData.num2 > arg2.itemData.num2) 
                {
                    return -1;
                }
                if (arg1.itemData.num2 < arg2.itemData.num2) 
                {
                    return 1;
                }
                return 0;
            }
            sortRobCity = (arg1: SGuildPlayerAttendanceVo, arg2: SGuildPlayerAttendanceVo): int
            {
                var loc1=arg1.itemData.num3 > 0 ? 1 : 0;
                var loc2=arg1.itemData.num4 > 0 ? 1 : 0;
                var loc3=arg1.itemData.num5 > 0 ? 1 : 0;
                var loc4=arg2.itemData.num3 > 0 ? 1 : 0;
                var loc5=arg2.itemData.num4 > 0 ? 1 : 0;
                var loc6=arg2.itemData.num5 > 0 ? 1 : 0;
                var loc7=loc1 + loc2 + loc3;
                var loc8=loc4 + loc5 + loc6;
                if (loc7 > loc8) 
                {
                    return -1;
                }
                if (loc7 < loc8) 
                {
                    return 1;
                }
                return 0;
            }
            var loc2=e.target.name;
            switch (loc2) 
            {
                case "contributeDescBtn":
                {
                    this._memberData.sort(this.sortContribute);
                    break;
                }
                case "leagueWarDescBtn":
                {
                    this._memberData.sort(sortLeagueWar);
                    break;
                }
                case "bossDescBtn":
                {
                    this._memberData.sort(sortBoss);
                    break;
                }
                case "robCityDescBtn":
                {
                    this._memberData.sort(sortRobCity);
                    break;
                }
            }
            this.onEquipmentPageChange();
            return;
        }

        /* internal  */sortContribute(arg1: SGuildPlayerAttendanceVo, arg2: SGuildPlayerAttendanceVo): int
        {
            if (arg1.itemData.totalContribute > arg2.itemData.totalContribute) 
            {
                return -1;
            }
            if (arg1.itemData.totalContribute < arg2.itemData.totalContribute) 
            {
                return 1;
            }
            return 0;
        }

        /* internal  */get singleAllocationView(): Object /* flash.display.Sprite */
        {
            if (this._singleAllocationView == null) 
            {
                this._singleAllocationView = new SingleAllocationView();
                this._singleAllocationView.x = 400;
                this._singleAllocationView.y = 28;
            }
            return this._singleAllocationView;
        }

        /* internal  */get multiAllocationView(): Object /* flash.display.Sprite */
        {
            if (this._multiAllocationView == null) 
            {
                this._multiAllocationView = new MultiAllocationView();
                this._multiAllocationView.x = 400;
                this._multiAllocationView.y = 28;
            }
            return this._multiAllocationView;
        }

        public setGuildWarehouseRecordsList(arg1: Array<any>): void
        {
            if (arg1) 
            {
                this._memberData = arg1;
                this._memberData.sort(this.sortContribute);
                this._pageTool.array = this._memberData;
                this._memberPageSelecter.currentPage = 1;
                this._memberPageSelecter.maxPage = this._pageTool.pagesCount;
                this._memberList.dataProvider = new DataProvider(this._pageTool.getByPageNo(1));
            }
            return;
        }

        public setGuildWarehouseItems(arg1: Array<any>): void
        {
            this._guildWarehouseItems = arg1;
            if (this._singleAllocationView && this.contains(this._singleAllocationView)) 
            {
                this._singleAllocationView.setGuildWarehouseItems(arg1);
            }
            return;
        }

        public setGuildWarehouseMoney(arg1: Map<any, any> /* flash.utils.Dictionary */): void
        {
            this._storageMoneyMap = arg1;
            if (this._singleAllocationView && this.contains(this._singleAllocationView)) 
            {
                this._singleAllocationView.setGuildWarehouseMoney(arg1);
            }
            else if (this._multiAllocationView && this.contains(this._multiAllocationView)) 
            {
                this._multiAllocationView.setGuildWarehouseMoney(arg1);
            }
            return;
        }

        public setGuildWarehouseReplyList(arg1: Array<any>): void
        {
            if (this._singleAllocationView && this.contains(this._singleAllocationView)) 
            {
                this._singleAllocationView.setGuildWarehouseReplyList(arg1);
            }
            return;
        }

        public removeReplyedApply(arg1: SGuildWarehouseApply): void
        {
            if (this._singleAllocationView && this.contains(this._singleAllocationView)) 
            {
                this._singleAllocationView.removeReplyedApply(arg1);
            }
            return;
        }

        public assignGuildWarehouseExSucceed(): void
        {
            if (this._singleAllocationView && this.contains(this._singleAllocationView)) 
            {
                this._singleAllocationView.onCleanHandler();
            }
            else if (this._multiAllocationView && this.contains(this._multiAllocationView)) 
            {
                this._multiAllocationView.onCleanHandler();
                this.cleanSelectState();
            }
            return;
        }

        /* internal  */init(): void
        {
            ObjCreate.createBg(0, 0, 400, 397, this);
            ObjCreate.createGTextField(Language.getString(27951), 45, 10, 80, 20, this, GlobalStyle.textFormat2);
            ObjCreate.createGTextField(Language.getString(27952), 130, 10, 80, 20, this, GlobalStyle.textFormat2);
            ObjCreate.createGTextField(Language.getString(27953), 200, 10, 80, 20, this, GlobalStyle.textFormat2);
            ObjCreate.createGTextField(Language.getString(27954), 260, 10, 80, 20, this, GlobalStyle.textFormat2);
            ObjCreate.createGTextField(Language.getString(27955), 330, 10, 80, 20, this, GlobalStyle.textFormat2);
            var loc1=ObjCreate.createGButton("", 170, 10, 11, 17, this, "DescRedButton");
            loc1.name = "contributeDescBtn";
            loc1.addEventListener(flash.events.MouseEvent.CLICK, this.onDescBtnClickHandler);
            var loc2=ObjCreate.createGButton("", 240, 10, 11, 17, this, "DescRedButton");
            loc2.name = "leagueWarDescBtn";
            loc2.addEventListener(flash.events.MouseEvent.CLICK, this.onDescBtnClickHandler);
            var loc3=ObjCreate.createGButton("", 312, 10, 11, 17, this, "DescRedButton");
            loc3.name = "bossDescBtn";
            loc3.addEventListener(flash.events.MouseEvent.CLICK, this.onDescBtnClickHandler);
            var loc4;
            (loc4 = ObjCreate.createGButton("", 382, 10, 11, 17, this, "DescRedButton")).name = "robCityDescBtn";
            loc4.addEventListener(flash.events.MouseEvent.CLICK, this.onDescBtnClickHandler);
            ObjCreate.createBg(0, 30, 400, 3, this, ImagesConst.SplitLine);
            this._memberList = ObjCreate.createTileList(0, 30, 400, 340, this);
            this._memberList.scrollPolicy = ScrollPolicy.OFF;
            this._memberList.columnWidth = 338;
            this._memberList.rowHeight = 33;
            this._memberList.setStyle("cellRenderer", GuildMemberItemRenderer);
            this._memberList.addEventListener(ListEvent.ITEM_CLICK, this.onSelectItemHandler);
            this._memberPageSelecter = ObjCreate.createPageSelecter(140, 373, this);
            this._memberPageSelecter.mode = PageSelecter.CompleteMode;
            this._memberPageSelecter.addEventListener(flash.events.Event.CHANGE, this.onEquipmentPageChange);
            var loc5;
            (loc5 = ObjCreate.createTextField(Language.getString(27968), 595, 6, 140, 20, this, null, true)).addEventListener(flash.events.TextEvent.LINK, this.onShowAllocationRecordPanel);
            this._tabBar = new GTabBar();
            this._tabBar.x = 410;
            this._tabBar.y = 0;
            this._tabBar.horizontalGap = 2;
            this._tabBar.buttonWidth = 70;
            this._tabBar.buttonFilters = [FilterConst.glowFilter];
            this._tabBar.selectedIndex = 0;
            this._tabBar.dataProvider = Language.getArray(27956);
            this._tabBar.addEventListener(MuiEvent.GTABBAR_SELECTED_CHANGE, this.onTabBarSelectedChange);
            this.addChild(this._tabBar);
            this.addChild(this.singleAllocationView);
            Dispatcher.addEventListener(EventName.MultiAllocationCancelItem, this.onCancelItem);
            return;
        }

        /* internal */ /* var */_memberList: GTileList;

        /* internal */ /* var */_memberPageSelecter: PageSelecter;

        /* internal */ /* var */_pageTool: PageTool;

        /* internal */ /* var */_tabBar: GTabBar;

        /* internal */ /* var */_singleAllocationView: SingleAllocationView;

        /* internal */ /* var */_multiAllocationView: MultiAllocationView;

        /* internal */ /* var */_memberData: Array<any>;

        /* internal */ /* var */_guildWarehouseItems: Array<any>;

        /* internal */ /* var */_storageMoneyMap: Map<any, any> /* flash.utils.Dictionary */;
    }
